import 'package:flutter/material.dart';
import '../models/word.dart';
import '../services/database_service.dart';

class ReviewScreen extends StatefulWidget {
  const ReviewScreen({super.key});

  @override
  _ReviewScreenState createState() => _ReviewScreenState();
}

class _ReviewScreenState extends State<ReviewScreen> {
  final DatabaseService _databaseService = DatabaseService();
  List<Word> _wordsToReview = [];
  int _currentIndex = 0;
  bool _showMeaning = false;

  @override
  void initState() {
    super.initState();
    _loadWordsToReview();
  }

  Future<void> _loadWordsToReview() async {
    final words = await _databaseService.getWords();
    // 简单的复习算法：优先显示复习次数少的单词
    words.sort((a, b) => a.reviewCount.compareTo(b.reviewCount));
    setState(() {
      _wordsToReview = words;
    });
  }

  void _flipCard() {
    setState(() {
      _showMeaning = !_showMeaning;
    });
  }

  Future<void> _markReviewed(bool remembered) async {
    if (_wordsToReview.isEmpty) return;

    Word currentWord = _wordsToReview[_currentIndex];
    currentWord.reviewCount++;
    currentWord.lastReviewed = DateTime.now();
    await _databaseService.updateWord(currentWord);

    setState(() {
      if (_currentIndex < _wordsToReview.length - 1) {
        _currentIndex++;
        _showMeaning = false;
      } else {
        // 复习完所有单词
        Navigator.pop(context);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    if (_wordsToReview.isEmpty) {
      return Scaffold(
        appBar: AppBar(title: const Text('复习单词')),
        body: const Center(child: Text('没有需要复习的单词')),
      );
    }

    final currentWord = _wordsToReview[_currentIndex];

    return Scaffold(
      appBar: AppBar(
        title: const Text('复习单词'),
        actions: [
          Center(
            child: Padding(
              padding: const EdgeInsets.only(right: 16.0),
              child: Text('${_currentIndex + 1}/${_wordsToReview.length}'),
            ),
          ),
        ],
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          GestureDetector(
            onTap: _flipCard,
            child: Card(
              margin: const EdgeInsets.all(16),
              child: Container(
                width: double.infinity,
                padding: const EdgeInsets.all(32),
                child: Column(
                  children: [
                    Text(
                      _showMeaning ? currentWord.meaning : currentWord.word,
                      style: const TextStyle(fontSize: 24),
                      textAlign: TextAlign.center,
                    ),
                    if (_showMeaning && currentWord.example != null)
                      Padding(
                        padding: const EdgeInsets.only(top: 16),
                        child: Text(
                          currentWord.example!,
                          style: const TextStyle(
                            fontSize: 16,
                            fontStyle: FontStyle.italic,
                          ),
                          textAlign: TextAlign.center,
                        ),
                      ),
                  ],
                ),
              ),
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(16),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                ElevatedButton(
                  onPressed: () => _markReviewed(false),
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.red,
                    foregroundColor: Colors.white,
                  ),
                  child: const Text('不认识'),
                ),
                ElevatedButton(
                  onPressed: () => _markReviewed(true),
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.green,
                    foregroundColor: Colors.white,
                  ),
                  child: const Text('认识'),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}